<template>
  <a-modal
    :width='900'
    :visible='visible'
    :confirmLoading='loading'
    @cancel="() => { $emit('cancel') }"
  >
    <!--    @ok="() => { $emit('ok') }"-->

    <template slot='title'>
      <span>{{ model ? '编辑页面' : '新增页面' }}</span>
    </template>
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <!--        <a-form-item label='选择模板'>-->
        <!--          <div class='template-list' v-decorator="[-->
        <!--              'templateId',-->
        <!--              { rules: [{ required: true, message: '请选择模板!' }] },-->
        <!--            ]">-->
        <!--            <div class='template-item' :class='{active: templateIndex === item }' v-for='item in 8' :key='item'-->
        <!--                 @click='handleSelectTemplate(item)'>-->
        <!--              <div class='img-container'>-->
        <!--                &lt;!&ndash;TODO:模板缩略图&ndash;&gt;-->
        <!--              </div>-->
        <!--              <p class='template-name'>空白模板</p>-->
        <!--            </div>-->
        <!--          </div>-->
        <!--        </a-form-item>-->
        <a-form-item label='主键' v-show='false'>
          <a-input v-decorator="['id']" />
        </a-form-item>

        <a-form-item label='页面名称'>
          <a-input v-decorator="[
              'pageName',
              { rules: [{ required: true, message: '请输入页面名称!' }, { pattern: namePattern,  message: '请输入合法内容(可输入0到50位（中文、字母，数字，下划线，减号)'}] },
            ]" />
        </a-form-item>

        <a-form-item label='是否可见'>
          <a-radio-group :default-value="1" v-decorator="['isHidden',{initialValue: 1}]" >
            <a-radio :value="0">
              否
            </a-radio>
            <a-radio :value="1">
              是
            </a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot='footer'>
      <a-button icon='highlight' type='primary' v-if='!!model' @click='$emit("design")'>
        设计
      </a-button>
      <a-button key='cancel' @click="$emit('cancel')">
        取消
      </a-button>
      <a-button key='submit' type='primary' @click="$emit('ok')">
        确定
      </a-button>
    </template>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { namePattern } from '@/reges/index'

const fields = ['id', 'pageName', 'isHidden']

export default {
  name: 'creatRealform',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  components: {},
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      templateIndex: 0,
      namePattern,
      form: this.$form.createForm(this)
    }
  },
  created() {
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handleSelectTemplate(index) {
      this.templateIndex = index
      this.form.setFieldsValue({
        ...pick(this.model, fields),
        templateId: this.templateIndex
      })
    }
  }
}
</script>

<style lang='less' scoped>
.template-list {
  display: flex;
  flex-wrap: wrap;
  height: 300px;
  overflow: auto;
}

.template-item {
  width: 156px;
  height: 105px;
  display: flex;
  flex-direction: column;
  box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
  margin: 15px;
  padding: 10px;
  cursor: pointer;

  &.active {
    border: solid 1px rgba(65, 171, 251, 1);
    box-shadow: rgba(65, 171, 251, 1) 0px 3px 8px;
  }

  .img-container {
    flex: 1;
    background-color: red;
  }

  .template-name {
    width: 156px;
    height: 18px;
    margin-bottom: 5px;
  }

  img {
    object-fit: cover;
    flex: 1;
  }
}
</style>